//
//  StatusCell.swift
//  glzbowei
//
//  Created by 高伦展 on 2018/1/7.
//  Copyright © 2018年 glz. All rights reserved.
//

import UIKit
import SnapKit

// 微博 cell 中控件的间距数值
let StatusCellMargin:CGFloat = 12

// 微博 头像的宽度
let StatusCellIconWidth:CGFloat = 35

// 微博 cell
class StatusCell: UITableViewCell {
    
    //微博视图模型
    var viewModel:StatusViewModel?{
        didSet{
            topView.viewModel = viewModel
            
            contentLabel.text = viewModel?.status.text
            
            //设置配图视图 - 设置视图模型之后，配图视图有能力计算大小
            picView.viewModel = viewModel
            
            picView.snp.updateConstraints { (make) in
                make.height.equalTo(picView.bounds.height)
                make.width.equalTo(picView.bounds.width)
                
//                let offset = (viewModel?.thumbnailUrls?.count ?? 0) > 0 ? StatusCellMargin:0
//                make.top.equalTo(contentLabel.snp.bottom).offset(offset)
            }
        }
    }
    
    //根据指定的视图模型计算行高
    func rowHeight(vm:StatusViewModel) -> CGFloat {
        //记录视图模型 -> 会调用上面的didSet 设置内容以及更新 ‘约束’
        viewModel = vm
        
        //强制更新所有约束 -> 所有控件的frame都会被计算正确
        contentView.layoutIfNeeded()
        
        //返回底部视图的最大高度
        let frame = bottomView.frame
        
        return frame.maxY
    }
    
    //MARK: - 构造函数
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
        
        selectionStyle = .none
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //顶部视图
    private lazy var topView : StatusCellTopView = StatusCellTopView()
    //微博正文标签
      lazy var contentLabel : UILabel =
        UILabel(title:"微博正文",
                fontSize:15,
                color:UIColor.darkGray,
                screenInset:StatusCellMargin)
    //缩略图
      lazy var picView :StatusPictureView = StatusPictureView()
    //底部视图
      lazy var bottomView : StatusCellButtomView = StatusCellButtomView()

}

//MARK: - 设置界面
extension StatusCell{
    
    @objc func setupUI()
    {
        //1.添加控件
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(picView)
        contentView.addSubview(bottomView)
        
        
//        contentLabel.preferredMaxLayoutWidth = UIScreen.main.bounds.width - 2 * StatusCellMargin
        
        //2.自动布局
        topView.snp.makeConstraints { (make) in
            make.top.equalTo(contentView.snp.top)
            make.left.equalTo(contentView.snp.left)
            make.right.equalTo(contentView.snp.right)
            
            make.height.equalTo(2*StatusCellMargin + StatusCellIconWidth)
        }
        
        contentLabel.snp.makeConstraints { (make) in
            make.top.equalTo(topView.snp.bottom).offset(StatusCellMargin)
            make.left.equalTo(contentView.snp.left).offset(StatusCellMargin)
            
        }
        
        bottomView.snp.makeConstraints { (make) in
            make.top.equalTo(picView.snp.bottom).offset(StatusCellMargin)
            make.left.equalTo(contentView.snp.left)
            make.right.equalTo(contentView.snp.right)
            make.height.equalTo(44)
            
            //make.bottom.equalTo(contentView.snp.bottom)
        }
        
    }
    
}
